
package org.qloudgen.sec.cas.server.authentication.principal;

import org.jasig.cas.authentication.Credential;
import org.jasig.cas.authentication.principal.PrincipalResolver;
import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.SimplePrincipal;
;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import org.qloudgen.sec.cas.support.user.services.User;
import org.qloudgen.sec.cas.support.user.services.UsersManager;

import javax.validation.constraints.NotNull;

public class MongoPrincipalResolver implements PrincipalResolver {

    private static final Logger logger = LoggerFactory.getLogger(MongoPrincipalResolver.class);
    
    /** Instance of dao. */
    @NotNull
    private UsersManager usersManager;

    public MongoPrincipalResolver(final UsersManager usersManager) {
    	  this.usersManager = usersManager;
    	  logger.warn("{} init.............",getClass().getName());
	  }


    @Override
    public Principal resolve(final Credential credential) {

        User user = getUserInformation(credential.getId());
        Map attributes = user.getCol();
        return new SimplePrincipal(credential.getId(),attributes);
    }

    private User getUserInformation(String username){
		User user = null;
		user = usersManager.findUserByName(username);
		logger.debug("find user by name {},user is {}",username,user);		
		return user;
	}
    @Override
    public boolean supports(final Credential credential) {
        return credential.getId() != null;
    }
}
